package com.passive.lock.controller.sys;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("logo")
public class LogoController {
	@RequestMapping("/upload")
	@ResponseBody
	public Result uploadLogo(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {

		String originalFilename = file.getOriginalFilename();
		InputStream bis = null;
		OutputStream bos = null;
		try {
			bis = file.getInputStream();
			bos = new FileOutputStream("src/main/resources/public/"+originalFilename);
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (final IOException e) {
			System.out.println("出现IOException." + e);
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
		return Result.ok();

	}
}
